package com.hst.capacity.application.biz.impl;

import com.alibaba.fastjson2.JSON;
import com.hst.capacity.application.biz.FranchiseeUserBiz;
import com.hst.capacity.domain.exception.BusinessException;
import com.hst.capacity.domain.model.entity.auth.UserEntity;
import com.hst.capacity.domain.model.entity.franchisee.FranchiseeUserEntity;
import com.hst.capacity.domain.model.request.RePasswordParam;
import com.hst.capacity.domain.model.request.franchisee.FranchiseeUserParam;
import com.hst.capacity.domain.model.request.franchisee.FranchiseeUserListParam;
import com.hst.capacity.domain.model.response.PagingResultVO;
import com.hst.capacity.domain.model.response.franchisee.FranchiseeUserVO;
import com.hst.capacity.domain.service.FranchiseeUserService;
import com.hst.capacity.domain.service.UserService;
import com.hst.capacity.infra.convertor.FranchiseeConvertor;
import com.hst.capacity.infra.persistence.jpa.UserRepository;
import com.hst.capacity.infra.persistence.mybatis.FranchiseeUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class FranchiseeUserBizImpl implements FranchiseeUserBiz {

    @Autowired
    private FranchiseeUserService franchiseeUserService;

    @Autowired
    private FranchiseeUserMapper userMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserRepository userRepository;

    @Override
    public String add(FranchiseeUserParam param) {
        // 添加用户账号
        return franchiseeUserService.save(param).getId();
    }

    @Override
    public String edit(FranchiseeUserParam param) {
        // 添加用户账号
        return franchiseeUserService.edit(param).getId();
    }

    @Override
    public PagingResultVO<FranchiseeUserVO> list(FranchiseeUserListParam param) {
//        return UserConvertor.INSTANCE.convert(franchiseeUserService.list(param));
        return new PagingResultVO<>(userMapper.listTotal(param), userMapper.list(param));
    }

    @Override
    public Boolean rePassword(RePasswordParam param) {
        UserEntity user = userService.authenticate(param.getUsername(), param.getPassword()).getUser();
        user.setUserPassword(passwordEncoder.encode(param.getNewPassword()));
        userRepository.save(user);
        return true;
    }

    @Override
    public FranchiseeUserVO select(String idFranchiseeUser) {
        return franchiseeUserService.select(idFranchiseeUser);
    }

    @Override
    public FranchiseeUserVO sreachByUserID(String userID) {
        UserEntity userEntity = userRepository.findById(userID).get();
        if (userEntity == null)
            throw new BusinessException(5002, "未找到该用户信息，请联系管理员");
        log.info("UserEntity:" + JSON.toJSONString(userEntity));
        FranchiseeUserEntity entity = franchiseeUserService.getFranchiseeUser(userEntity);
        if (entity == null)
            throw new BusinessException(5002, "未找到该用户信息，请联系管理员");
        log.info("FranchiseeUserEntity:" + JSON.toJSONString(entity));
        return FranchiseeConvertor.INSTANCE.convert(entity);
    }

}
